<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="企业" prop="enterpriseInformation.id"
                :rules="[
                  {required: true, message:'企业不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择企业"
            labelName = 'companyName'
            labelValue = 'id'
            :value = "inputForm.enterpriseInformation.id"
            :limit="1"
            @getValue='(value) => {inputForm.enterpriseInformation.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'companyName',
              label: '企业名称'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/list"
            entityBeanName="enterpriseInformation"
            queryEntityUrl="/dlyrl/hr/enterpriseinformation/enterpriseInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="用户名" prop="userName"
                :rules="[
                  {required: true, message:'用户名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.userName" placeholder="请填写用户名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="身份证号" prop="identity"
                :rules="[
                  {required: true, message:'身份证号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.identity" placeholder="请填写身份证号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="身份证姓名" prop="identityName"
                :rules="[
                  {required: true, message:'身份证姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.identityName" placeholder="请填写身份证姓名"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="手机号" prop="phone"
                :rules="[
                  {required: true, message:'手机号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写手机号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="电子邮件" prop="email"
                :rules="[
                  {required: true, message:'电子邮件不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.email" placeholder="请填写电子邮件"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="登录密码" prop="password"
                :rules="[
                  {required: true, message:'登录密码不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.password" placeholder="请填写登录密码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="审核状态" prop="allowType"
                :rules="[
                  {required: true, message:'审核状态不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.allowType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('allowtype')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="注册类型" prop="typeOfRegistration"
                :rules="[
                 ]">
                <el-select v-model="inputForm.typeOfRegistration" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('type_registration')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="企业用户角色" prop="enterpriseUserRoles.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择企业用户角色"
            labelName = 'roleName'
            labelValue = 'id'
            :value = "inputForm.enterpriseUserRoles.id"
            :limit="1"
            @getValue='(value) => {inputForm.enterpriseUserRoles.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'roleName',
              label: '角色名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/list"
            entityBeanName="enterpriseRoleInformation"
            queryEntityUrl="/dlyrl/hr/enterpriserole/enterpriseRoleInformation/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="公司职务" prop="post.id"
                :rules="[
                  {required: true, message:'公司职务不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择公司职务"
            labelName = 'jobName'
            labelValue = 'id'
            :value = "inputForm.post.id"
            :limit="1"
            @getValue='(value) => {inputForm.post.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'jobName',
              label: '岗位名'
            }
            ]"
            :searchs="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'jobName',
              label: '岗位名'
            }
            ]"
            dataListUrl="/dlyrl/hr/enterprisejobs/enterpriseJobs/list"
            entityBeanName="enterpriseJobs"
            queryEntityUrl="/dlyrl/hr/enterprisejobs/enterpriseJobs/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- (0:是管理员；1:不是管理员) -->
            <el-form-item label="是否为管理员" prop="administrator"
                :rules="[
                  {required: true, message:'是否为管理员(不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.administrator" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('administrator')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="融云用户id" prop="rongyunuserid"
                :rules="[
                  {required: true, message:'融云用户id不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.rongyunuserid" placeholder="请填写融云用户id"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="融云token" prop="rongyuntoken"
                :rules="[
                  {required: true, message:'融云token不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.rongyuntoken" placeholder="请填写融云token"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="登录token" prop="token"
                :rules="[
                 ]">
              <el-input v-model="inputForm.token" placeholder="请填写登录token"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="部门机构" prop="institutions.id"
                :rules="[
                  {required: true, message:'部门机构不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="institutions"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/dlyrl/hr/orporateinstitutions/orporateInstitutions/treeData"
                      :value="inputForm.institutions.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.institutions.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="支付密码" prop="paymentCode"
                :rules="[
                  {required: true, message:'支付密码不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.paymentCode" placeholder="请填写支付密码"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="企业用户头像" prop="headPortait"
                :rules="[
                 ]">
              <el-upload ref="headPortait"
                    v-if="visible"
                    :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation`"
                    :headers="{token: $cookie.get('token')}"
                    :on-preview="(file, fileList) => {$window.location.href = (file.response && file.response.url) || file.url}"
                    :on-success="(response, file, fileList) => {
                       inputForm.headPortait = fileList.map(item => (item.response && item.response.url) || item.url).join('|')
                    }"
                    :on-remove="(file, fileList) => {
                      $http.post(`/sys/file/webupload/deleteByUrl?url=${(file.response && file.response.url) || file.url}`).then(({data}) => {
                        $message.success(data.msg)
                      })
                      inputForm.headPortait = fileList.map(item => item.url).join('|')
                    }"
                    :before-remove="(file, fileList) => {
                      return $confirm(`确定移除 ${file.name}？`)
                    }"
                    multiple
                    :limit="5"
                    :on-exceed="(files, fileList) =>{
                      $message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
                    }"
                    :file-list="headPortaitArra">
                    <el-button size="small" type="primary">点击上传</el-button>
                    <div slot="tip" class="el-upload__tip">添加相关附件</div>
                  </el-upload>
           </el-form-item>
        </el-col>
        
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        headPortaitArra: [],
        inputForm: {
          id: '',
          enterpriseInformation: {
            id: ''
          },
          userName: '',
          identity: '',
          identityName: '',
          phone: '',
          email: '',
          password: '',
          allowType: '',
          typeOfRegistration: '',
          headPortait: '',
          enterpriseUserRoles: {
            id: ''
          },
          post: {
            id: ''
          },
          administrator: '',
          rongyunuserid: '',
          rongyuntoken: '',
          token: '',
          institutions: {
            id: ''
          },
          paymentCode: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect,
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建企业用户信息`
        } else if (method === 'edit') {
          this.title = '修改企业用户信息'
        } else if (method === 'view') {
          this.title = '查看企业用户信息'
        }
        this.headPortaitArra = []
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.enterpriseUserInformation)
              this.inputForm.headPortait.split('|').forEach((item) => {
                if (item.trim().length > 0) {
                  this.headPortaitArra.push({name: decodeURIComponent(item.substring(item.lastIndexOf('/') + 1)), url: item})
                }
              })
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/dlyrl/hr/enterpriseuserinformation/enterpriseUserInformation/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>